<?php
namespace Home\Controller;
use Think\Controller;
class RegisterController extends Controller {

    public function Register(){
        layout(false);
        $this->display();
    }

    public function buildVerify(){
    	$Verify = new \Think\Verify();
    	$Verify->fontSize = 50;
    	$Verify->length = 4;
    	$Verify->entry();
    }

    public function checkInput(){
    	if(IS_AJAX){
    		$willCheckName = array_keys($_POST)[0];  //得到想检查的inputID
    		$this->email = $_POST[$willCheckName];
    		if($willCheckName == 'email'){  //如果检查的是email 就调用checkEmail方法
    			$this->ajaxReturn($this->checkEmail($willCheckName, $_POST[$willCheckName])); 
    		}

    		//如果检查的是密码
    		if($willCheckName == 'password'){
    			$this->ajaxReturn($this->checkPassword($willCheckName, $_POST[$willCheckName]));
    		}

    		//如果检测的是验证码
    		if($willCheckName == 'verify'){
    			$this->ajaxReturn($this->checkVerify($willCheckName, $_POST[$willCheckName]));
    		}


    	}
    }


    public function checkEmail($checkname, $value){
    	$emailPreg = '/^([0-9A-Za-z\\-_\\.]+)@([0-9a-z]+\\.[a-z]{2,3}(\\.[a-z]{2})?)$/i';
    	//判断邮箱格式是否符合要求
    	if(!preg_match($emailPreg, $value)){
			$returnMsg = array(
				'error'     => 1,
				'content'   => '格式不正确',
				'returnObj' => $checkname,
			);    		
    	}else{
    		$sql = "select * from user where email = '{$value}'";
    		$num = count(M('user')->query($sql));
    		if( $num == 1 ){
    			$returnMsg = array(
					'error'     => 2,
					'content'   => '邮箱已经存在',
					'returnObj' => $checkname,
				);
    		}elseif( $num == 0 ){
    			$returnMsg = array(
					'error'     => 0,
					'content'   => '邮箱可用',
					'returnObj' => $checkname,
				);
    		}
    		
    	}
    	return $returnMsg;
    }

    public function checkPassword($checkname, $value){
    	$passwordPreg = '/\w{6}/';
    	if(preg_match($passwordPreg, $value)){
    		$returnMsg = array(
				'error'     => 0,
				'content'   => '密码符合',
				'returnObj' => $checkname,
			);
    	}else{
    		$returnMsg = array(
				'error'     => 1,
				'content'   => '长度不够',
				'returnObj' => $checkname,
			);
    	}
    	return $returnMsg;

    }

    public function checkVerify($checkname, $value){
    	$Verify = new \Think\Verify();
    	$Verify->reset = false;
    	if( $Verify->check($value)){
    		$returnMsg = array(
				'error'     => 0,
				'content'   => '验证码正确',
				'returnObj' => $checkname,
			);
    	}else{
    		$returnMsg = array(
				'error'     => 1,
				'content'   => '验证码错误',
				'returnObj' => $checkname,
			);
    	}
    	return $returnMsg;
    }

    //全部认证通过则提交
    public function allowRegister(){
    	if($_POST){
    		
    		$table = M('user');
    		$data = $table->create();  //得到表单的值
    		$data['reg_time'] = time();
    		$data['reg_ip'] = $_SERVER['SERVER_ADDR'];  //获得Cilent IP 地址

    		$keyArr  = array_keys($data);				//获得键
    		$valuArr = array_values($data);				//获得键的对应值
    		$keyStr = implode(',', $keyArr);			//拼接成字符串
    		$valueStr = implode("','" ,$valuArr);		//拼接成值得字符串
    		$valueStr = "'".$valueStr."'";				//把值得字符串前后添加 单引号'

    		$sql = "insert into user({$keyStr}) values({$valueStr})";  //组成sql语句
    		
    		if($table->add($data)){
    			$this->success('注册成功',U('Home/Register/Register'));
    		}
    		
    	}else{
    		$this->error('非法操作',U('Home/Register/Register'));
    	}
    }




    //登录
    public function allowLogin(){
    	if($_POST){
    		$email = $_POST['usermail'];
	    	$password = $_POST['userpassword'];
	    	$sql = "select * from user where email = '{$email}' and password = '{$password}'";
	    	$sum = count(M('user')->query($sql));
	    	if($sum){
                
                $_SESSION['uid'] = M('user')->where("email = '{$email}' and password = '{$password}'")->getField('id');
                $_SESSION['email'] = M('user')->where("id = {$_SESSION['uid']}")->getField('email');
                $_SESSION['face'] = M('user')->where("email = '{$email}' and password = '{$password}'")->getField('face');
                // session('email',null);       //登录成功之后 清空email的session 释放资源
	    		$this->success('登录成功',U('Home/Index/index'));
	    	}else{
	    		$this->error('登录失败',U('Home/Register/Register'));
	    	}
    	}else{
    		$this->error('非法访问',U('Home/Register/Register'));
    	}
    	
    }

    //检测登录
    public function checkLogin(){
    	$willCheckName = array_keys($_POST)[0];  //得到想检查的inputID
    	if($willCheckName == 'usermail'){
    		$this->ajaxReturn($this->checkUserMail($willCheckName,$_POST[$willCheckName]));
    	}
    	if($willCheckName == 'userpassword'){
    		$this->ajaxReturn($this->checkUserPwd($willCheckName,$_POST[$willCheckName]));
    	}
    }

    //检测用户登录的邮箱
    public function checkUserMail($checkname,$value){
    	$emailPreg = '/^([0-9A-Za-z\\-_\\.]+)@([0-9a-z]+\\.[a-z]{2,3}(\\.[a-z]{2})?)$/i';
    	if(!preg_match($emailPreg, $value)){
    		$returnMsg = array(
				'error'     => 1,
				'content'   => '格式不正确',
				'returnObj' => $checkname,
			);
    	}else{
    		$table = M('user');
    		$sql = "select * from user where email = '{$value}'";
    		$num = count($table->query($sql));
    		if($num){
    			//保留sesesion 方便做 检索邮箱密码是否存在记录 登录成功之后就清空对一个的session
    			$_SESSION['email'] = $value;
    			$returnMsg = array(
					'error'     => 0,
					'content'   => '邮箱存在',
					'returnObj' => $checkname,
				);
    		}else{
    			$returnMsg = array(
					'error'     => 2,
					'content'   => '邮箱不存在',
					'returnObj' => $checkname,
				);
    		}
    	}

    	return $returnMsg;
    }

    public function checkUserPwd($checkname,$value){
    	$table = M('user');
		$sql = "select * from user where email = '{$_SESSION['email']}' and password = '{$value}'";
		$sum = count($table->query($sql));
		if($sum){
			$returnMsg = array(
				'error'     => 0,
				'content'   => '密码正确',
				'returnObj' => $checkname,
			);
		}else{
			$returnMsg = array(
				'error'     => 1,
				'content'   => '密码错误',
				'returnObj' => $checkname,
			);
		}

		return $returnMsg;
    }


    public function loginOut(){
        session(null);
        $this->success('退出成功',U('Home/Register/Register'));
    }


    public function qqLogin(){
       $res =  qqLoginApi(); 

    }

    //QQ登录授权之后返回QQ的回调信息
    public function qqLoginCallback(){
        $qqReturnRes = qqLoginReturn();
        $open_id  = $qqReturnRes['openid'];
        //查询这个唯一的openid在数据库中是否存在
        $sql = "select * from user where qq_open_id = '{$open_id}'";
        if(M()->query($sql)){
            
        }else{
            layout('layout');
            $table = M('category');
            $result = $table->select();
            $this->assign('data', $result);
            $this->assign('qqReturnRes',$qqReturnRes);
            $this->display();
        }
    }

    public function bindPersonInfo(){
        $data['email'] = $_POST['email'];
        $data['password'] = $_POST['name'];
        // $data['face'] = $_POST['face'];
        $data['qq_open_id'] = $_POST['openid'];
        $data['reg_time'] = time();
       
        if(M('user')->add($data)){
            $_SESSION['uid'] = M('user')->where("qq_open_id = '{$data['qq_open_id']}'")->getField('id');
            $_SESSION['email'] = M('user')->where("id = {$_SESSION['uid']}")->getField('email');
            $_SESSION['face'] = M('user')->where("qq_open_id = '{$data['qq_open_id']}'")->getField('face');
            $this->success('绑定成功',U('home/register/register'));
        }else{
            $this->error('绑定失败',U('home/register/register'));
        }

    }
    
}